package com.fs.dao.Impl;

import com.fs.dao.QuestionAnswsersDao;
import com.fs.pojo.QuestionAnswsers;
import com.fs.utils.JDBCUtils;
import com.fs.utils.MyConnectionPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class QuestionAnswsersDaoImpl implements QuestionAnswsersDao {

    @Override
    public Integer getTotalScoreInClassPaperByStuId(String classPaperId, String stuId) {

        String sql = "select sum(score) from tb_question_answsers where class_paper_id=? and stu_id=?";

        Integer totalScores = null;

        Connection connection = null;
        try {
            connection = MyConnectionPool.getConnection();
            PreparedStatement prep = connection.prepareStatement(sql);

            prep.setString(1,classPaperId);
            prep.setString(2,stuId);

            ResultSet resultSet = prep.executeQuery();

            if(resultSet.next())
                totalScores = resultSet.getInt(1);

            prep.close();
            connection.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return totalScores;
    }

    @Override
    public QuestionAnswsers queryStudentAnswer(String stuId, String classPaperId, String questId) {

        String sql = "select * from tb_question_answsers where stu_id=? and class_paper_id=? and quesiton_id=?";

        List<QuestionAnswsers> questionAnswsers = JDBCUtils.executeQurey(sql, QuestionAnswsers.class, stuId, classPaperId, questId);

        return questionAnswsers == null ? null : questionAnswsers.get(0);
    }

    @Override
    public QuestionAnswsers queryById(String id) {
        return null;
    }

    @Override
    public List<QuestionAnswsers> queryAll() {
        return null;
    }

    @Override
    public int insert(QuestionAnswsers obj) {
        return 0;
    }

    @Override
    public int deleteById(String id) {
        return 0;
    }

    @Override
    public int update(QuestionAnswsers obj) {
        return 0;
    }
}
